<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MyThriftClient{
	private $client = null;
	private $userIp = null;
	private $clientName = null;
	
	function __construct($clientName, $input, $output=null, $userIp=null){
		$this->clientName = $clientName;
		$this->client = new $clientName($input, $output);
		$this->userIp = $userIp;
	}

	function __call($name, $args){
		$rt = false;
		$ms = microtime(true);
		try{
			$rt = call_user_func_array(array($this->client, $name), $args);
		}catch(Exception $e){
			$clientName = $this->clientName;
			log_message('error',"Thrift: Exception in $clientName.$name: ".$e->getMessage());
		}
		$timeCost = (microtime(true) - $ms) * 1000;
		$timeCost = number_format($timeCost, 2, '.', '');
		$str = $this->userIp.' - ';
		$str.= $timeCost.' - ';
		$str.= $name.' - ';
		$str.= json_encode($args);
		$str.= ' - ';
		$str.= json_encode($rt);
		log_message('info',$this->clientName.': '.$str);
		return $rt;
	}
}